package eerbaar.client.internal.ctr.operator.area;

import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IScope;
import eerbaar.client.exp.ScopeManager;
import eerbaar.client.internal.ctr.operator.Global;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.IPosition;

public class ValueAreaPopulator {

    private final IGame game;
    private final IExp[] expressions;
    private final ValueArea valueArea;

    public ValueAreaPopulator(ValueArea valueArea, IGame game, IExp[] array) {

        this.expressions = array;
        this.game = Global.copyGame(game);
        this.valueArea = valueArea;
    }

    public IPosition calcDestination(IScope scope) {

        IPersona pet = (IPersona) scope.getTarget();
        return this.valueArea.findHeighest(pet.getPosition(), 1);
    }

    public ValueArea getValueArea() {
        return this.valueArea;
    }

    public void populateValueArea(IScope scope) {

        IPersona target = (IPersona) scope.getTarget();
        for (IExp expression : this.expressions) {

            ScopeManager manager = scope.getScopeManager();

            IScope subScope = manager.pushScope("sub");
            subScope.setGame(this.game);
            subScope.setValueArea(this.valueArea);
            subScope.setTarget(target);

            expression.updateArea(subScope);
            manager.popScope();
        }
    }

}
